<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="网站名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="button_left" type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" />
        <el-table-column prop="name" label="网站名称" />
        <el-table-column prop="creator" label="创建人" />
        <el-table-column prop="createAt" label="创建时间" />
        <el-table-column prop="isDelete" align="center" label="开关">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isDelete"
              active-color="#4d75cc"
              inactive-color="#E9EBF2"
              active-value="0"
              inactive-value="1"
              @change="changeEnabled(scope.row, scope.row.isDelete)"
            />
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','sysWebsiteManage:edit','sysWebsiteManage:del'])" label="操作" width="150px" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudJob from '@/api/system/sysWebsiteManage'
import crudSysWebsiteManage from '@/api/system/sysWebsiteManage'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import pagination from '@crud/Pagination'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'

const defaultForm = { id: undefined, name: undefined, creatorId: undefined, createAt: undefined, updateAt: undefined, isDelete: '0', deptId: undefined }
export default {
  name: 'SysWebsiteManage',
  components: { crudOperation, pagination, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '/修改网站', url: 'api/website-manage', idField: 'id', sort: 'id,desc', crudMethod: { ...crudSysWebsiteManage }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'sysWebsiteManage:add'],
        edit: ['admin', 'sysWebsiteManage:edit'],
        del: ['admin', 'sysWebsiteManage:del']
      },
      rules: {
        name: [
          { required: true, message: '网站名称不能为空', trigger: 'blur' }
        ],
        creatorId: [
          { required: true, message: '创建人不能为空', trigger: 'blur' }
        ],
        createAt: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ],
        updateAt: [
          { required: true, message: '更新时间不能为空', trigger: 'blur' }
        ],
        isDelete: [
          { required: true, message: '删除状态不能为空', trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: '部门id不能为空', trigger: 'blur' }
        ]
      }}
  },
  created() {
    console.log(this.form)
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 改变状态
    changeEnabled(data, val) {
      // const vals = Number(val)
      console.log(val, data)
      this.$confirm('此操作将 "' + val === 0 ? '此操作将开' : '此操作将关' + '" ' + data.name + '网站, 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // eslint-disable-next-line no-undef
        crudJob.edit(data).then(() => {
          // eslint-disable-next-line no-undef
          this.crud.notify('成功', 'success')
        }).catch(err => {
          data.isDelete = !data.isDelete
          console.log(err.data.message)
        })
      }).catch(() => {
        data.isDelete = !data.isDelete
      })
    }
  }
}
</script>

<style scoped lang="scss">
.dialog-footer{
  .button_left{
    margin-right: 20px;
  }
}
</style>
